package com.fgs.common.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.fgs.common.application.CommonApplication;
import com.fgs.common.utils.DateUtil;


/**
 * @author fgs
 * @date 2020/3/27
 **/
public class CommonSharedPreferenceManager {

    private static final String SHARED_USER_INFO = "userInfo";
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private CommonSharedPreferenceManager() {
        sharedPreferences = CommonApplication.getContext().getSharedPreferences(SHARED_USER_INFO, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static CommonSharedPreferenceManager getInstance() {
        return SingletonHolder.instance;
    }

    public String getToken() {
        return sharedPreferences.getString("token", "");
    }

    private static class SingletonHolder {
        private static CommonSharedPreferenceManager instance = new CommonSharedPreferenceManager();
    }


    public void clear() {
////        editor.clear();
//        editor.putString("phone", "");
//        editor.commit();
    }

    /**
     * 同意协议
     *
     * @param isAgree
     */
    public void setAgreement(boolean isAgree) {
        editor.putBoolean("isAgree", isAgree);
        editor.commit();
    }

    /**
     * 同意协议
     */
    public boolean getAgreement() {
        boolean isAgree = sharedPreferences.getBoolean("isAgree", false);
        return isAgree;
    }


    /**
     * 当天不需要提示升级
     */
    public void setWithUpdateDate() {
        String date = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");
        editor.putString("date", date);
        editor.commit();
    }

    /**
     * 当天不需要提示升级
     */
    public boolean getNeedUpdateDate() {
        String date = sharedPreferences.getString("date", "");
        String currentDate = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");
        if (!TextUtils.isEmpty(date) && !TextUtils.isEmpty(currentDate)) {
            return !date.equals(currentDate);
        }
        return true;

    }

    public void setPhoneInfo(String phoneInfo) {
        editor.putString("phoneInfo", phoneInfo);
        editor.commit();
    }

    public String getPhoneInfo() {
        String phoneInfo = sharedPreferences.getString("phoneInfo", "");
        return phoneInfo;
    }

    public void setOnePhone(String onePhone) {
        editor.putString("onePhone", onePhone);
        editor.commit();
    }

    public String getOnePhone() {
        String phoneInfo = sharedPreferences.getString("onePhone", "");
        return phoneInfo;
    }



}
